package org.jetnuke.dao.generic;

import org.apache.log4j.Logger;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;

import net.jforum.JForumExecutionContext;
import net.jforum.dao.generic.AutoKeys;
import net.jforum.util.preferences.SystemGlobals;

import org.jetnuke.dao.PictureVoteDAO;
import org.jetnuke.entities.PictureVote;

public class GenericPictureVoteDAO extends AutoKeys implements PictureVoteDAO {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger
			.getLogger(GenericPictureVoteDAO.class);

	public PictureVote retrieveByPK(int id) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("retrieveByPK(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("PictureVoteModel.selectByID"));
		p.setInt(1, id);

		ResultSet rs = p.executeQuery();
		PictureVote s = new PictureVote();

		rs.next();
		fillPictureVoteFromResultSet(s, rs);

		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("retrieveByPK(int) - end"); //$NON-NLS-1$
		}
		return s;
	}

	protected void fillPictureVoteFromResultSet(PictureVote s, ResultSet rs)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("fillPictureVoteFromResultSet(PictureVote, ResultSet) - start"); //$NON-NLS-1$
		}

		s.setVOTE_AUTHOR(rs.getInt("VOTE_AUTHOR"));
		s.setPID(rs.getInt("PID"));
		s.setVOTE(rs.getInt("VOTE"));
		s.setVOTE_DATE(rs.getTimestamp("VOTE_DATE"));
		s.setVOTEID(rs.getInt("VOTE_ID"));

		if (logger.isDebugEnabled()) {
			logger
					.debug("fillPictureVoteFromResultSet(PictureVote, ResultSet) - end"); //$NON-NLS-1$
		}
	}

	protected List processSelectAll(ResultSet rs) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("processSelectAll(ResultSet) - start"); //$NON-NLS-1$
		}

		List list = new ArrayList();
		while (rs.next()) {
			PictureVote u = new PictureVote();
			fillPictureVoteFromResultSet(u, rs);
			list.add(u);
		}

		if (logger.isDebugEnabled()) {
			logger.debug("processSelectAll(ResultSet) - end"); //$NON-NLS-1$
		}
		return list;
	}

	public int addNew(PictureVote pa) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("addNew(PictureVote) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = this
				.getStatementForAutoKeys("PictureVoteModel.addNew");
		this.setAutoGeneratedKeysQuery(SystemGlobals
				.getSql("PictureVoteModel.lastGeneratedPictureVoteID"));
		initPictureVote(pa, p);

		int id = this.executeAutoKeysQuery(p);
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("addNew(PictureVote) - end"); //$NON-NLS-1$
		}
		return id;

	}

	protected void initPictureVote(PictureVote pa, PreparedStatement p)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("initPictureVote(PictureVote, PreparedStatement) - start"); //$NON-NLS-1$
		}

		GregorianCalendar gc = new GregorianCalendar();
		long initTime = gc.getTimeInMillis();
		Timestamp stamp = new Timestamp(initTime);

		p.setInt(1, pa.getVOTE_AUTHOR());
		p.setInt(2, pa.getVOTE());
		p.setTimestamp(3, stamp);
		p.setInt(4, pa.getPID());

		if (logger.isDebugEnabled()) {
			logger
					.debug("initPictureVote(PictureVote, PreparedStatement) - end"); //$NON-NLS-1$
		}
	}

	public List selectByPicture(int pid) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectByPicture(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals
								.getSql("PictureVoteModel.selectByPicture"));
		p.setInt(1, pid);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectByPicture(int) - end"); //$NON-NLS-1$
		}
		return list;
	}

	public List selectLastVoted(int limit) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectLastVoted(int) - start"); //$NON-NLS-1$
		}

		// TODO Auto-generated method stub

		if (logger.isDebugEnabled()) {
			logger.debug("selectLastVoted(int) - end"); //$NON-NLS-1$
		}
		return null;
	}

	public boolean alreadyVoted(int pid, int uid) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("alreadyVoted(int, int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("PictureVoteModel.countVotes"));
		p.setInt(1, pid);
		p.setInt(2, uid);

		ResultSet rs = p.executeQuery();
		rs.next();
		int ret = rs.getInt(1);
		rs.close();
		p.close();
		if (ret > 0)
			return true;

		if (logger.isDebugEnabled()) {
			logger.debug("alreadyVoted(int, int) - end"); //$NON-NLS-1$
		}
		return false;
	}

	public void deleteByPID(int pid) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("deleteByPID(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("PictureVoteModel.deleteByPID"));
		p.setInt(1, pid);
		p.executeUpdate();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("deleteByPID(int) - end"); //$NON-NLS-1$
		}
	}
}
